package com.yumeno.market_goods.controller.pages.back;

import com.yumeno.market_goods.consts.GoodsConsts;
import com.yumeno.market_goods.controller.pages.BasicController;
import com.yumeno.market_goods.pojo.entity.Goods;
import com.yumeno.market_goods.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/pages/back/goods")
public class GoodsController extends BasicController {

    @Resource
    private GoodsService goodsService;

    @RequestMapping("/addPre")
    String addPre() {
        return "pages/back/goods/goods-addPre";
    }

    @RequestMapping("/add")
    @ResponseBody
    Map<String,Object> add(Goods goods,MultipartFile pic) {
        if ("on".equals(goods.getOnSale())){
            goods.setOnSale("YES");
        } else if ("off".equals(goods.getOnSale())){
            goods.setOnSale("NO");
        }
        String imgUrl = uploadFile("/upload/goods/", pic);
        goods.setImg(imgUrl);
        return goodsService.add(goods);
    }

}
